package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingPlanProjectMapper nursingPlanProjectMapper;
    @Override
    public PageResponse<NursingProjectVo> queryPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingProjectVo> page=nursingProjectMapper.pageBuConditional(name,status);
        page.stream().forEach(s->s.setCount(nursingPlanProjectMapper.selectByProjectId(s.getId())));
        return PageResponse.of(page,NursingProjectVo.class);
    }

    @Override
    public int addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject np=new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,np);
        return nursingProjectMapper.insertNp(np);
    }

    @Override
    public NursingProjectVo getNpById(Long id) {
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        NursingProject nursingProject=nursingProjectMapper.getById(id);

        if (nursingProject != null) {
            BeanUtils.copyProperties(nursingProject,nursingProjectVo);
            return nursingProjectVo;
        }else {
            throw  new BaseException(BasicEnum.NURSINGPROJECT_ID_EMPTY);
        }


    }

    @Override
    public int updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject np=new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,np);
        return nursingProjectMapper.updateByID(np);
    }

    @Override
    public int deleteNursingProjectByID(Long id) {
        //TODO：判断是否关联计划
        //关联，不让删除
        //不关联，删除
        return nursingProjectMapper.deleteByID(id);
    }

    @Override
    public int updateStauts(Long id, Integer status) {

        return nursingProjectMapper.updateStatus(id,status);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        return nursingProjectMapper.getAll();
    }
}
